package com.coach.service;

import java.awt.geom.Area;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import org.javasimon.aop.Monitored;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coach.dao.StudentDAO;
import com.coach.dao.UserDAO;
import com.coach.entity.Student;
import com.coach.entity.User;
import com.coach.util.BaseResponse;


@Transactional
@Service
@Monitored
public class UserService {
	
	@Autowired
	private UserDAO userDAO;
	
	
	@Autowired
	private StudentDAO studentDAO;
	

	 
	public BaseResponse<User> getUserList(Map<String, Object> map){		
		BaseResponse<User> response = new BaseResponse<User>();
	    try {
	        List<User> list = userDAO.getUserList(map);
	        response.setSuccess(true);
	        response.setData(list);

	    } catch(Exception e) {
	        e.printStackTrace();
	        response.setSuccess(false);
	    }
	    return response;
	}
	
	public BaseResponse<Integer> checkPhoneNumberExist(String phoneNumber){
		BaseResponse<Integer> response=new BaseResponse<Integer>();
		try{
			List<Integer> list=new ArrayList<Integer>();
			int a=userDAO.checkPhoneNumberExist(phoneNumber);
			list.add(a);
			response.setSuccess(true);
			response.setData(list);
		}catch(Exception e){
			e.printStackTrace();
			response.setSuccess(false);
		}
		
		return response;
	}
	
	
	public BaseResponse<User> checkLogin(Map<String, Object> map){		
		BaseResponse<User> response = new BaseResponse<User>();
	    try {
	        List<User> list = userDAO.checkLogin(map);	       	        
	        response.setSuccess(true);
	        response.setData(list);

	    } catch(Exception e) {
	        e.printStackTrace();
	        response.setSuccess(false);
	    }
	    return response;
	}
	
	public BaseResponse<User> addUser(User u){		
		BaseResponse<User> response = new BaseResponse<User>();
	    try {
	        userDAO.addUser(u);
	        response.setSuccess(true);
	        response.setData(null);

	    } catch(Exception e) {
	        e.printStackTrace();
	        response.setSuccess(false);
	    }
	    return response;
	}
	
	public BaseResponse<User> updateUser(User u){		
		BaseResponse<User> response = new BaseResponse<User>();
	    try {
	        userDAO.updateUser(u);
	        response.setSuccess(true);
	        response.setData(null);

	    } catch(Exception e) {
	        e.printStackTrace();
	        response.setSuccess(false);
	    }
	    return response;
	}
	
	public BaseResponse<User> deleteUser(User u){		
		BaseResponse<User> response = new BaseResponse<User>();
	    try {
	        userDAO.deleteUser(u);
	        response.setSuccess(true);
	        response.setData(null);

	    } catch(Exception e) {
	        e.printStackTrace();
	        response.setSuccess(false);
	    }
	    return response;
	}

    public User getUserById(int id){
    	  User u=null;
    	try{
    		u=userDAO.getUserById(id);
    		
    	}catch (Exception e) {
    		   e.printStackTrace();
		}
    	
    	return u;
    }
    
    
    /*
         **以下为studentDAO
     */
    
    public Student getStudentById(int id){
    	Student s=null;
    	try{
    		s=studentDAO.getStudentById(id);
    	}catch(Exception e){
           e.printStackTrace();
    	}
    	return s;
    }
	 
    
	public BaseResponse<Student> updateStudent(Map<String,Object> map){		
		BaseResponse<Student> response = new BaseResponse<Student>();
	    try {
	        studentDAO.updateStudent(map);
	        response.setSuccess(true);
	        response.setData(null);

	    } catch(Exception e) {
	        e.printStackTrace();
	        response.setSuccess(false);
	    }
	    return response;
	}
	
	public BaseResponse<Student> deleteStudent(int id){		
		BaseResponse<Student> response = new BaseResponse<Student>();
	    try {
	        studentDAO.deleteStudent(id);
	        response.setSuccess(true);
	        response.setData(null);

	    } catch(Exception e) {
	        e.printStackTrace();
	        response.setSuccess(false);
	    }
	    return response;
	}
	
	public BaseResponse<Student> addStudent(Student u){		
		BaseResponse<Student> response = new BaseResponse<Student>();
	    try {
	        studentDAO.addStudent(u);
	        response.setSuccess(true);
	        response.setData(null);

	    } catch(Exception e) {
	        e.printStackTrace();
	        response.setSuccess(false);
	    }
	    return response;
	}
    

}
